package com.itheima.web;

import com.itheima.bean.Dd;
import com.itheima.bean.ShoppingCart;
import com.itheima.result.R;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.MyBaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R list(){
        //调用service层的方法进行添加菜品到购物车
        List<ShoppingCart> s = shoppingCartService.updateList();
        //将查询到的集合返回到前端页面
        return R.success(s);

    }

    @PostMapping("add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        //掉用service层的方法添加购物车中的菜品
        ShoppingCart s = shoppingCartService.add(shoppingCart);
        return R.success(s);
    }

    @DeleteMapping("/clean")
    public R delete(){
        //调用service层的方法进行删除
        int row = shoppingCartService.delete(MyBaseContext.getThreadLong());
        //根据row影响行数判断是否添加成功
        if (row > 0){
            return R.success("清除成功！");
        }else {
            return R.error("清除失败！");
        }
    }

    @PostMapping("/sub")
    public R sub(@RequestBody Dd dishId){
        /*System.out.println("map = " + map);*/
        //调用service的修改方法对代码进行修改
        ShoppingCart shoppingCart = shoppingCartService.update(dishId.getDishId());
        //判断返回修改的影响行数，响应给前端相应的数据
        if (shoppingCart != null){
            return R.success(shoppingCart);
        }else {
            return R.success("");
        }
       /* return null;*/
    }
}
